package shuanfa.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
//插入排序
public class InsertSort {
    public static void main(String[] args) {
        int[] arr=new int[]{3,4,6,7,2,7,2,8,0};
        int[] array = new int[80000];
        for (int i = 0; i < 80000; i++) {
            array[i]=(int)(Math.random()*80000);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        System.out.println("排序前时间：" + format);
        insertSort(arr);
        System.out.println(Arrays.toString(arr));
        String format1 = simpleDateFormat.format(new Date());
        System.out.println("排序后的时间：" + format1);
    }

    private static void insertSort(int[] array) {
        int preIndex ,current;//current 保存当前arr[i]数据
        for (int i = 1; i < array.length; i++) {
            current = array[i];
            preIndex = i-1;
            while (preIndex >=0 && array[preIndex]>current){
                array[preIndex +1] = array[preIndex];//前一个索引数据后移
                preIndex--;
            }
            array[preIndex +1] = current;
        }
    }
}
